package top.wshape1.shiziapi.uaa.json.deserializer;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-08-31
 */

public class OAuth2AuthorizationRequestJsonDeserializer extends JsonDeserializer<OAuth2AuthorizationRequest> {

    @Override
    public OAuth2AuthorizationRequest deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
        TreeNode treeNode = p.getCodec().readTree(p);

        String authorizationUri = eval(treeNode.get("authorizationUri").toString());
        String clientId = eval(treeNode.get("clientId").toString());
        String redirectUri = eval(treeNode.get("redirectUri").toString());
        String state = eval(treeNode.get("state").toString());
        String authorizationRequestUri = eval(treeNode.get("authorizationRequestUri").toString());

        HashSet<String> scopes = new HashSet<>(1);
        TreeNode scopesTree = treeNode.get("scopes");
        for (int i = 0; i < scopesTree.size(); i++) {
            scopes.add(eval(scopesTree.get(i).toString()));
        }

        HashMap<String, Object> additionalParameters = new HashMap<>();
        TreeNode parametersTree = treeNode.get("additionalParameters");
        Iterator<String> parametersIterator = parametersTree.fieldNames();
        String field;
        while (parametersIterator.hasNext()) {
            field = parametersIterator.next();
            additionalParameters.put(field, eval(parametersTree.get(field).toString()));
        }

        HashMap<String, Object> attributes = new HashMap<>();
        TreeNode attributesTree = treeNode.get("attributes");
        Iterator<String> attributesIterator = attributesTree.fieldNames();
        while (attributesIterator.hasNext()) {
            field = attributesIterator.next();
            attributes.put(field, eval(attributesTree.get(field).toString()));
        }
//            String grantTypeValue = treeNode.get("authorizationGrantType").get("value").toString();
//            AuthorizationGrantType authorizationGrantType = new AuthorizationGrantType(grantTypeValue);
//
//            String responseTypeValue = treeNode.get("responseType").get("value").toString();
//            OAuth2AuthorizationResponseType responseType = new OAuth2AuthorizationResponseType(responseTypeValue);
        return OAuth2AuthorizationRequest.authorizationCode()
                .authorizationRequestUri(authorizationRequestUri)
                .authorizationUri(authorizationUri)
                .clientId(clientId)
                .redirectUri(redirectUri)
                .state(state)
                .scopes(scopes)
                .attributes(attributes)
                .additionalParameters(additionalParameters)
                .build();
    }

    private String eval(String str) {
        if (str.length() > 2 && str.startsWith("\"") && str.endsWith("\""))
            return str.substring(1, str.length() - 1);
        return str;
    }


}
